﻿// See https://aka.ms/new-console-template for more information
using DataGenerate;
using SqlSugar;
using System.Text;
using System.Text.Json;

Console.WriteLine("Hello, World!");

var pathByPermission = "C:\\wingy\\YKZN.OTA\\YKZN.OTA.Api\\wwwroot\\BlogCore.Data.json\\Permission.tsv";
var pathByModules = "C:\\wingy\\YKZN.OTA\\YKZN.OTA.Api\\wwwroot\\BlogCore.Data.json\\Modules.tsv";
var pathByRoleModulePermission = "C:\\wingy\\YKZN.OTA\\YKZN.OTA.Api\\wwwroot\\BlogCore.Data.json\\RoleModulePermission.tsv";

using var fsByPermission = new FileStream(pathByPermission, FileMode.Open, FileAccess.Read);
using var srByPermission = new StreamReader(fsByPermission);
var strJsonByPermission = srByPermission.ReadToEnd();
var permissionList = JsonSerializer.Deserialize<List<Permission>>(strJsonByPermission);

using var fsByModules = new FileStream(pathByModules, FileMode.Open, FileAccess.Read);
using var srByModules = new StreamReader(fsByModules);
var strJsonByModules = srByModules.ReadToEnd();
var modulesList = JsonSerializer.Deserialize<List<Modules>>(strJsonByModules);

using var fsByRoleModulePermission = new FileStream(pathByRoleModulePermission, FileMode.Open, FileAccess.Read);
using var srByRoleModulePermission = new StreamReader(fsByRoleModulePermission);
var strJsonByRoleModulePermission = srByRoleModulePermission.ReadToEnd();
var roleModulePermissionList = JsonSerializer.Deserialize<List<RoleModulePermission>>(strJsonByRoleModulePermission);

foreach (var roleModulePermissionItem in roleModulePermissionList.Where(x => x.RoleId == 4))
{
    foreach (var permissionItem in permissionList.Where(x => x.Id == roleModulePermissionItem.PermissionId))
    {
        var permissionId = SnowFlakeSingle.Instance.NextId();
        foreach (var permissionChildItem in permissionList.Where(x => x.Pid == permissionItem.Id && x.Pid != 0))
        {
            permissionChildItem.Pid = permissionId;
            permissionChildItem.CreateTime = DateTime.Parse("2024-01-01 00:00:00");
            permissionChildItem.CreateId = 1666998806811648000;
        }
        permissionItem.Id = permissionId;
        permissionItem.CreateId = 1666998806811648000;
        permissionItem.CreateTime = DateTime.Parse("2024-01-01 00:00:00");
        roleModulePermissionItem.PermissionId = permissionId;
    }

    foreach (var modulesItem in modulesList.Where(x => x.Id == roleModulePermissionItem.ModuleId))
    {
        var moduleId = SnowFlakeSingle.Instance.NextId();
        foreach (var item in modulesList.Where(x => x.ParentId == modulesItem.Id && x.ParentId != 0))
        {
            item.ParentId = moduleId;
            item.CreateTime = DateTime.Parse("2024-01-01 00:00:00");
            item.CreateId = 1666998806811648000;
        }
        modulesItem.Id = moduleId;
        modulesItem.CreateId = 1666998806811648000;
        modulesItem.CreateTime = DateTime.Parse("2024-01-01 00:00:00");
        roleModulePermissionItem.ModuleId = moduleId;
    }
    roleModulePermissionItem.Id = SnowFlakeSingle.Instance.NextId();
    roleModulePermissionItem.RoleId = 1673606563786530816;
    roleModulePermissionItem.CreateId = 1666998806811648000;
    roleModulePermissionItem.CreateTime = DateTime.Parse("2024-01-01 00:00:00");
}

foreach (var item in permissionList.Where(x => x.Id.ToString().Length < 3).ToList())
{
    permissionList.Remove(item);
}

foreach (var item in modulesList.Where(x => x.Id.ToString().Length < 3 && x.ParentId.ToString().Length < 3).ToList())
{
    modulesList.Remove(item);
}

foreach (var item in roleModulePermissionList.Where(x => x.RoleId != 1673606563786530816).ToList())
{
    roleModulePermissionList.Remove(item);
}

var pathResponseByPermission = "C:\\wingy\\YKZN.OTA\\YKZN.OTA.Api\\wwwroot\\BlogCore.Data.json\\Permission_test.tsv";
var pathResponseByModules = "C:\\wingy\\YKZN.OTA\\YKZN.OTA.Api\\wwwroot\\BlogCore.Data.json\\Modules_test.tsv";
var pathRoleModulePermission = "C:\\wingy\\YKZN.OTA\\YKZN.OTA.Api\\wwwroot\\BlogCore.Data.json\\RoleModulePermission_test.tsv";
using (FileStream fsResponseByPermission = File.Create(pathResponseByPermission))
{
    byte[] permissionBytes = new UTF8Encoding(true).GetBytes(JsonSerializer.Serialize(permissionList));
    fsResponseByPermission.Write(permissionBytes, 0, permissionBytes.Length);
}

using (FileStream fsResponseByModule = File.Create(pathResponseByModules))
{
    byte[] moduleBytes = new UTF8Encoding(true).GetBytes(JsonSerializer.Serialize(modulesList));
    fsResponseByModule.Write(moduleBytes, 0, moduleBytes.Length);
}

using (FileStream fsRoleModulePermission = File.Create(pathRoleModulePermission))
{
    byte[] roleModulePermissionBytes = new UTF8Encoding(true).GetBytes(JsonSerializer.Serialize(roleModulePermissionList));
    fsRoleModulePermission.Write(roleModulePermissionBytes, 0, roleModulePermissionBytes.Length);
}
Console.ReadLine();
